package com.liao.orders.filter;

/**
 * @author 28580
 * @version 1.0
 * @description: TODO
 * @date 2023/7/23 10:29
 */
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.liao.base.base.po.MovieUsers;
import com.liao.base.base.utils.ThreadLocalUtils;
import com.liao.base.base.utils.UserContextHolder;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Order(-1000)
@Component
public class TokenAuthenticationFilter extends OncePerRequestFilter {

    // 重写  doFilterInternal  方法即可
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
        // 获取请求中header的 x-client-token-user信息
        String user = httpServletRequest.getHeader("x-client-token-user");
        if (user != null) {
            MovieUsers jsonObject = JSON.parseObject(user, MovieUsers.class);


            ThreadLocalUtils.set(jsonObject);
        } else {
            UserContextHolder.getInstance().setContext(null);
        }

        filterChain.doFilter(httpServletRequest, httpServletResponse);
    }
}
